<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <header th:replace="header::html"></header>
</head>
<body>
    <form class="layui-form layui-form-pane" action="/user/addUser" method="post">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" required  lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name" required lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block">
                <select name="role" lay-verify="">
                    <option value="">请选择一个角色</option>
                    <option value="root">root</option>
                    <option value="admin">admin</option>
                    <option value="user">user</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">编号</label>
            <div class="layui-input-block">
                <input type="text" name="number" required lay-verify="required" placeholder="请输入编号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <script>
        //Demo
        layui.use('form', function(){
            var form = layui.form;

            //监听提交
            form.on('submit(formDemo)', function(data){
                $ = layui.jquery;
                $.ajax({
                    url:"/user/addUser",    //数据接口
                    type:"POST",            //提交方式
                    contentType: "application/json; charset=utf-8",        //定义网络文件的类型和网页的编码，决定浏览器将以什么形式、什么编码读取这个文件
                    data:JSON.stringify(data.field),
                    dataType:'json',        //数据类型
                    success:function(result){
                        layer.alert("添加成功", {icon: 6},function (index1) {
                            layer.close(index1);
                            location.reload()       //当前页面刷新
                            parent.location.reload();   //父级页面刷新
                            var index = parent.layer.getFrameIndex(window.name);    //获取当前页面的索引值(被父级索引的值)
                            parent.layer.close(index);  //关闭子页面
                        });
                    }
                })
                return false;
            });
        });
    </script>
</body>
</html>